#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'AirTestUtils.php';
require_once 'models/TestOrganization.php';

// init
AIR2_DBManager::init();
$browser = new AirHttpTest();
$browser->set_test_user();

// create a test organization to look at
$o = new TestOrganization();
$o->save();
$uuid = $o->org_uuid;

plan(68);

/**********************
 * Validate HTML inline data
 */
$browser->set_content_type(AirHttpTest::$HTML); // set to html
ok( $resp = $browser->http_get("/organization/$uuid"), 'nav to orgpage' );
is( $browser->resp_code(), 200, "orgpage resp code" );

$var = air2_get_json_variable('AIR2.Organization.ORGUUID', $resp);
is( $var, $uuid, 'ORGUUID = $o->org_uuid' );
$var = air2_get_json_variable('AIR2.Organization.ORGURL', $resp);
is( $var, $browser->base_url."organization/$uuid", 'ORGURL set correctly' );

$var = air2_get_json_variable('AIR2.Organization.ORGDATA', $resp);
validate_json($var, 'ORGDATA');
$var = air2_get_json_variable('AIR2.Organization.CHDDATA', $resp);
validate_json($var, 'CHDDATA');
$var = air2_get_json_variable('AIR2.Organization.PRJDATA', $resp);
validate_json($var, 'PRJDATA');
$var = air2_get_json_variable('AIR2.Organization.USRDATA', $resp);
validate_json($var, 'USRDATA');
$var = air2_get_json_variable('AIR2.Organization.INQDATA', $resp);
validate_json($var, 'INQDATA');
$var = air2_get_json_variable('AIR2.Organization.ACTDATA', $resp);
validate_json($var, 'ACTDATA');


/**********************
 * Validate JSON data
 */
$browser->set_content_type(AirHttpTest::$JSON); // set to json

ok( $resp = $browser->http_get("/organization/$uuid"), 'json nav to org' );
is( $browser->resp_code(), 200, "json org resp code" );
validate_json($resp, 'org');
$json = json_decode($resp, true);
ok( key_exists('parent', $json['radix']), 'org parent set' );

ok( $resp = $browser->http_get("/organization/$uuid/projects"), 'json nav to projects' );
is( $browser->resp_code(), 200, "json projects resp code" );
validate_json($resp, 'projects');

ok( $resp = $browser->http_get("/organization/$uuid/users"), 'json nav to users' );
is( $browser->resp_code(), 200, "json users resp code" );
validate_json($resp, 'users');

ok( $resp = $browser->http_get("/organization/$uuid/inquiries"), 'json nav to inquiries' );
is( $browser->resp_code(), 200, "json inquiries resp code" );
validate_json($resp, 'inquiries');

ok( $resp = $browser->http_get("/organization/$uuid/activity"), 'json nav to activity' );
is( $browser->resp_code(), 200, "json activity resp code" );
validate_json($resp, 'activity');

ok( $resp = $browser->http_get("/organization/$uuid/children"), 'json nav to children' );
is( $browser->resp_code(), 200, "json children resp code" );
validate_json($resp, 'children');


/**********************
 * Check that "logo" returns an image
 */
$browser->set_content_type('image');
ok( $resp = $browser->http_get("/organization/$uuid/logo"), 'img nav to logo' );
is( $browser->resp_code(), 200, "img logo resp code" );
$type = $browser->resp_content_type();
ok( preg_match('/image/', $type), "img logo resp type" );


?>
